package com.mall.service.order.service;


import ch.qos.logback.core.joran.action.IADataForComplexProperty;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.model.domain.OmsOrderReturnReason;
import com.mall.model.dto.ReasonPageDto;
import com.mall.model.vo.PageResult;
import com.mall.service.order.mapper.OmsOrderReturnReasonMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

@Service
public class OmsOrderReturnReasonService {
    @Autowired
    private OmsOrderReturnReasonMapper omsOrderReturnReasonMapper;
    public void save(OmsOrderReturnReason omsOrderReturnReason) {
        omsOrderReturnReasonMapper.insert(omsOrderReturnReason);
    }

    public String  delete( List<Integer> ids) {
        for (Integer id : ids) {
            LambdaQueryWrapper<OmsOrderReturnReason>qw=new LambdaQueryWrapper<>();
            qw.eq(OmsOrderReturnReason::getId,id);
            OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectOne(qw);
            if(omsOrderReturnReason==null){
                return "已删除,请勿重复操作";
            }
            omsOrderReturnReasonMapper.deleteById(id);
        }
        return "删除成功";
    }

    public PageResult list(Integer pageNum, Integer pageSize) {
        IPage<OmsOrderReturnReason>page=new Page<>(pageNum,pageSize);
        QueryWrapper<OmsOrderReturnReason>QW=new QueryWrapper<>();
        IPage<OmsOrderReturnReason> omsOrderReturnReasonIPage = omsOrderReturnReasonMapper.selectPage(page, QW);
        PageResult pageResult = new PageResult();
pageResult.setList(omsOrderReturnReasonIPage.getRecords());
pageResult.setPageNum(pageNum);
pageResult.setPageSize(pageSize);
pageResult.setTotal(Math.toIntExact(page.getTotal()));
pageResult.setTotalPage(Math.toIntExact(page.getCurrent()));
//        ReasonPageDto reasonPageDto=new ReasonPageDto();
//        BeanUtils.copyProperties(omsOrderReturnReasonIPage,reasonPageDto);
//        reasonPageDto.setTotal(Math.toIntExact(page.getTotal()));
//        reasonPageDto.setTotalPage(Math.toIntExact(page.getCurrent()));
        return pageResult;

    }

    public void update(Integer id, OmsOrderReturnReason reasonPageDto) {

        omsOrderReturnReasonMapper.deleteById(id);
       omsOrderReturnReasonMapper.insert(reasonPageDto);
    }

    public OmsOrderReturnReason select(Integer id) {
        OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(id);
        return omsOrderReturnReason;
    }

    public void status(List<Integer> ids, Integer status) {
        for (Integer id : ids) {
            OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(id);
            omsOrderReturnReason.setStatus(status);
        }
    }
}
